<?php
namespace app\admin\controller\shop;

use app\admin\controller\Addon;
use think\facade\Request;
use app\common\controller\AdminController;
use app\admin\model\shop\Spec as SpecModel;
use app\admin\model\shop\SpecItem;
use app\admin\model\shop\Category as CategoryModel;
use think\App;
use think\facade\Db;
// use app\admin\model\ManagementStoreProductValue;
// use app\admin\model\ManagementStoreProduct;
/**
 * @ControllerAnnotation(title="mall_spec")
 * NotParse
 */
class Spec extends AdminController
{    
    use \app\admin\traits\Curd;

    protected $sort = [
    
        'sort' => 'asc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SpecModel();
        $this->SpecItem=new SpecItem();
        $this->cate=new CategoryModel();
        // $this->productValue=new ManagementStoreProductValue();
        // $this->product=new ManagementStoreProduct();
    
      
    }
    public function index(){
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model->where($where)->count();
            $list  = $this->model->where($where)->order($this->sort)->page($page, $limit)->select();
            foreach ($list as $k => $v) {
                $content=$this->SpecItem->where("spec_id",$v['id'])->column('item');
                $list[$k]['spec']=implode(',',$content);
                $cate_name=$this->cate->where('cate_id',$v['cate_id'])->value('cate_name');
                $list[$k]['cate_name']=$cate_name?$cate_name:"";
            }
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    public function add(){
        if ($this->request->isAjax()) {
            $post = $this->request->post();
             $rule = [
                'cate_id|cate_id'    => 'require',
                'content|规格值'     => 'require',
            ];
            $this->validate($post, $rule);
         
            try {
            
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
       
        return $this->fetch();
    }
    public function edit()
    {
        $id  = Request::param('id');
        $row = $this->model->find($id);

        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $content=$this->SpecItem->where("spec_id",$id)->column('item');
        $row->content=implode(',',$content);
        $this->assign('row', $row);
        return $this->fetch();
    }
     /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        $itemId=$this->SpecItem->where("spec_id",$id)->column('id');
        $cate_id=$this->model->where('id',$id)->value('cate_id');
      
        // $productId=$this->product->where('cate_id',$cate_id)->column('products_id');
     
        // if($productId){
            // $this->error('该规格名已关联商品禁止删除！！！');
            // $productValue=$this->productValue->whereIn('products_id',$productId)->column('sku');
            // if($productValue){
            //     $str=explode(',',implode(',',$productValue));
            //     $count=array_intersect($itemId,$productValue);
            //     if($count){
            //         $this->error('该规格名已关联商品禁止删除！！！');
            //     }
            // }
        // }   
        
        try {
            $save = $row->delete();
           
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $save ? $this->success('删除成功') : $this->error($e->getMessage());
    }
    //所选分类下的规格
    public function getspec(){
        $post =Request::param();
        $where['cate_id']=$post['cate_id'];
        $list  = $this->model->field('id,title')->order('sort asc')->where($where)->select()->toArray();
        foreach ($list as $k => $v) {
            $item=Db::name('goods_spec_item')->where('spec_id',$v['id'])->order('id asc')->group('item')->column('item');
            $list[$k]['content']=implode(',',$item);
        }
        $data  = [
            'code'  => 1,
            'msg'   => '',
            'data'  => $list,
        ];
         return json($data);
    }
}
